package com.chainyoung.component_tmperson.ui.activity.account;

import android.content.Intent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.appcommon.entity.RecieveAccount;
import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.ui.mvp.contract.RecieveAccountListContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.RecieveAccountListPresenter;

import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;

import static com.chainyoung.router.RouterConstants.PATH_RECIEVE_ACCOUNT;

//收付款地址

@Route(path = PATH_RECIEVE_ACCOUNT)
public class RecieveAccountActivity extends BaseActivity<RecieveAccountListPresenter> implements RecieveAccountListContract.View {


    @BindView(R2.id.sw_bank)
    Switch swBank;
    @BindView(R2.id.sw_ali)
    Switch swAli;
    @BindView(R2.id.sw_weixin)
    Switch swWeixin;


    private boolean isHaveBank, isHaveAli, isHaveWexin;//是否已绑定银行 阿里，微信账户

    private final int TYPE_BANK = 1;
    private final int TYPE_ALI = 2;
    private final int TYPE_WEXIN = 3;
    private RecieveAccount aliAccount = null;
    private RecieveAccount wxAccount = null;


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        setCurrentTitle(R.string.tmperson_recieve_account_addr);
        setListeners();

    }

    private void setListeners() {
        swBank.setOnCheckedChangeListener(new MyOnCheckedChangeListener(TYPE_BANK));
        swAli.setOnCheckedChangeListener(new MyOnCheckedChangeListener(TYPE_ALI));
        swWeixin.setOnCheckedChangeListener(new MyOnCheckedChangeListener(TYPE_WEXIN));
    }

    private void setAccountStatus(int type, boolean isChecked) {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        map.put("type", type);
        map.put("open_status", isChecked ? 1 : 0);
        mPresenter.setRecieveAccountStatus(map);
    }


    @Override
    protected void initData() {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        mPresenter.getRecieveAccount(map);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_recieveaccount;
    }

    @OnClick({R2.id.rl_bank, R2.id.rl_ali, R2.id.rl_weixin})
    void onClick(View v) {
        if (v.getId() == R.id.rl_bank) {
            if (isHaveBank) {
                launch(MyBankCardActivity.class);
            } else {
                launch(BindBankCardActivity.class);
            }

        } else if (v.getId() == R.id.rl_ali) {
            Intent intent = new Intent(mContext, BindAccountCodeActivity.class);
            intent.putExtra("type", 2);
            intent.putExtra("account", aliAccount);
            mContext.startActivity(intent);

        } else if (v.getId() == R.id.rl_weixin) {
            Intent intent = new Intent(mContext, BindAccountCodeActivity.class);
            intent.putExtra("type", 3);
            intent.putExtra("account", wxAccount);
            mContext.startActivity(intent);
        }
    }


    @Override
    public void onGetRecieveAccount(List<RecieveAccount> data) {
        if (data == null) {
            return;
        }
        if (!data.isEmpty()) {
            for (RecieveAccount account : data) {
                checkAccountOpen(account);
            }
        }
        swBank.setEnabled(isHaveBank);
        swAli.setEnabled(isHaveAli);
        swWeixin.setEnabled(isHaveWexin);
    }

    @Override
    public void onSetRecieveAccountStatus(String data) {
        //showMsg(data);
    }

    private void checkAccountOpen(RecieveAccount account) {
        switch (account.getType()) {
            case 1:
                isHaveBank = true;
                swBank.setChecked(account.getOpen() != 0);
                break;
            case 2:
                isHaveAli = true;
                aliAccount = account;
                swAli.setChecked(account.getOpen() != 0);
                break;
            case 3:
                isHaveWexin = true;
                wxAccount = account;
                swWeixin.setChecked(account.getOpen() != 0);
                break;
        }
    }

    class MyOnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {

        private int type;

        MyOnCheckedChangeListener(int type) {
            this.type = type;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            setAccountStatus(type, isChecked);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }
}
